package org.file5.util;

import java.io.UnsupportedEncodingException;
import java.security.spec.InvalidKeySpecException;

import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;

public class DesEncrypter {
	
	private static String[] deskeys = {"&*^^8dfx"};
	
    Cipher ecipher;
    Cipher dcipher;

    public DesEncrypter( ) {
        this(deskeys[0]);
    }

    public DesEncrypter(SecretKey key) {
        try {
            ecipher = Cipher.getInstance("DES");
            dcipher = Cipher.getInstance("DES");
            ecipher.init(Cipher.ENCRYPT_MODE, key);
            dcipher.init(Cipher.DECRYPT_MODE, key);

        } catch (javax.crypto.NoSuchPaddingException e) {
        } catch (java.security.NoSuchAlgorithmException e) {
        } catch (java.security.InvalidKeyException e) {
        }
    }

    public DesEncrypter(String keyStr) {
        try {
            DESKeySpec   desKeySpec   =   new   DESKeySpec(keyStr.getBytes());
            SecretKeyFactory   keyFactory   =   SecretKeyFactory.getInstance( "DES");
            SecretKey   deskey   =   keyFactory.generateSecret(desKeySpec);
        	
            ecipher = Cipher.getInstance("DES");
            dcipher = Cipher.getInstance("DES");
            ecipher.init(Cipher.ENCRYPT_MODE, deskey);
            dcipher.init(Cipher.DECRYPT_MODE, deskey);

        } catch (javax.crypto.NoSuchPaddingException e) {
        } catch (java.security.NoSuchAlgorithmException e) {
        } catch (java.security.InvalidKeyException e) {
        } catch (InvalidKeySpecException e) {
		}
    }

    public String encrypt(String str) {
        try {
            // Encode the string into bytes using utf-8
            byte[] utf8 = str.getBytes("UTF8");

            // Encrypt
            byte[] enc = ecipher.doFinal(utf8);

            // Encode bytes to Hex to get a string
            return Hex.encodeHexString(enc);
            
        } catch (javax.crypto.BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        } catch (UnsupportedEncodingException e) {
        } 
        
        return null;
    }

    public String decrypt(String str) {
        try {
            // Decode hex to get bytes
            byte[] dec = Hex.decodeHex(str.toCharArray());

            // Decrypt
            byte[] utf8 = dcipher.doFinal(dec);
     
            // Decode using utf-8
            return new String(utf8, "UTF8");
        } catch (javax.crypto.BadPaddingException e) {
        } catch (IllegalBlockSizeException e) {
        } catch (UnsupportedEncodingException e) {
       
		} catch (DecoderException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        return null;
    }
    

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {
		    // Generate a temporary key. In practice, you would save this key.
		    // See also Encrypting with DES Using a Pass Phrase.
//		    SecretKey key = KeyGenerator.getInstance("DES").generateKey();
	 
            DESKeySpec   desKeySpec   =   new   DESKeySpec(deskeys[0].getBytes());
            SecretKeyFactory   keyFactory   =   SecretKeyFactory.getInstance( "DES");
            SecretKey   deskey   =   keyFactory.generateSecret(desKeySpec);
            
		    // Create encrypter/decrypter class
		    DesEncrypter encrypter = new DesEncrypter(deskey);

		    // Encrypt
		    String encrypted = encrypter.encrypt("chunhaichen@gmail.com");

		    // Decrypt
		    String decrypted = encrypter.decrypt(encrypted);
		    System.out.println(encrypted+"-----"+decrypted);
		} catch (Exception e) {
		}

	}

}
